package com.shchuangzhi.smartguard.core.command;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.shchuangzhi.smartguard.core.Consts;
import com.shchuangzhi.smartguard.core.Mode;
import com.shchuangzhi.smartguard.core.NotifyMessage;
import java.io.InputStream;

/**
 * Created by j_hu on 7/21/17.
 */

public class SyncAutoModeSwitchTimeHandler extends SyncMetaCommandRespHandler {
    public SyncAutoModeSwitchTimeHandler(SyncMetaCommand cmd) {
        super("auto.switch.time.sync", cmd);
    }

    @Override
    public boolean handle(InputStream input) {
        boolean handleOk = false;
        try {
            Gson gson = new Gson();
            SyncMetaCommandRS<SyncAutoModeSwitchTime> syncRS = (SyncMetaCommandRS<SyncAutoModeSwitchTime>)gson.fromJson(
                    getString(input),
                    new TypeToken<SyncMetaCommandRS<SyncAutoModeSwitchTime>>(){}.getType());
            if (syncRS != null && syncRS.getData() != null) {
                super.getCommand().notify((NotifyMessage.info(Consts.INFO_SYNC_AUTO_SWITCH_TIME_OK,
                        Consts.NOTIFY_DATA_SYNC, Consts.NOTIFY_DATA_SYC_VIEW_LIST)));
                Mode.getInstance().saveChangedConfig(syncRS.getData().isEnabled(), syncRS.getData().getTime());
                getCommand().getDbHelper().getSyncRecord().update(getName(), syncRS.getLastMetaUpdateTime());
                handleOk = true;
            }
        }catch(Exception e) {
            getCommand().raiseError(e);
        }
        return handleOk;
    }

    public class SyncAutoModeSwitchTime {
        private boolean enabled;
        private String time;

        public boolean isEnabled() {
            return enabled;
        }

        public String getTime() {
            return time;
        }
    }
}
